package org.cid54.android.jshopper.dao.impl;

import android.os.AsyncTask;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.cid54.android.jshopper.dao.DaoCallBackMethod;
import org.cid54.android.jshopper.dao.ShopItemDao;
import org.cid54.android.jshopper.data.CsvFetcher;
import org.cid54.android.jshopper.data.impl.CsvFetcherImpl;
import org.cid54.android.jshopper.model.ShopItem;
import org.cid54.android.jshopper.model.ShoppingList;

/**
 *
 * @author fgo
 */
public class ShopItemDaoImpl implements ShopItemDao {

    private final String shoppingListsSubUrlStart = "shoppingLists/";
    private CsvFetcher fetcher = new CsvFetcherImpl();

    public void getShopItemsByShoppingList(ShoppingList shoppingList, final DaoCallBackMethod<List<ShopItem>> callBack) {

        AsyncTask<ShoppingList, Void, List<ShopItem>> task = new AsyncTask<ShoppingList, Void, List<ShopItem>>() {

            @Override
            protected List<ShopItem> doInBackground(ShoppingList... shoppingLists) {
                List<ShopItem> resultL = new ArrayList<ShopItem>();

                String url = shoppingListsSubUrlStart + shoppingLists[0].getId();

                List<String[]> strings = fetcher.getData(url);

                for (String[] line : strings) {

                    ShopItem item = new ShopItem();

                    item.setId(Long.parseLong(line[0]));
                    item.setName(line[1]);
                    item.setAmount(Integer.parseInt(line[2]));
                    item.setShoppingList(shoppingLists[0]);
                    item.setBought(Boolean.parseBoolean(line[3]));

                    resultL.add(item);

                }

                return resultL;
            }

            @Override
            protected void onPostExecute(List<ShopItem> result) {
                super.onPostExecute(result);
                callBack.execute(result);
            }
        };

        task.execute(shoppingList);

    }

    public void create(ShopItem shopItem, final DaoCallBackMethod<Boolean> callBack) {
        AsyncTask<ShopItem, Void, Boolean> task = new AsyncTask<ShopItem, Void, Boolean>() {

            @Override
            protected Boolean doInBackground(ShopItem... ShopItems) {

                ShopItem sl = ShopItems[0];

                Map<String, String> argsMap = new HashMap<String, String>();

                argsMap.put("name", sl.getName());
                argsMap.put("amount", sl.getAmount() + "");

                return fetcher.postData(shoppingListsSubUrlStart + sl.getShoppingList().getId() + "/create", argsMap);

            }

            @Override
            protected void onPostExecute(Boolean result) {
                super.onPostExecute(result);
                callBack.execute(result);
            }
        };

        task.execute(shopItem);
    }

    public void update(ShopItem item, final DaoCallBackMethod<Void> callBack) {

        AsyncTask<ShopItem, Void, Boolean> task = new AsyncTask<ShopItem, Void, Boolean>() {

            @Override
            protected Boolean doInBackground(ShopItem... ShopItems) {

                ShopItem sl = ShopItems[0];

                Map<String, String> argsMap = new HashMap<String, String>();

                return fetcher.postData("items/" + sl.getId() + "/toggle", argsMap);

            }

            @Override
            protected void onPostExecute(Boolean result) {
                super.onPostExecute(result);
                callBack.execute(null);
            }
        };

        task.execute(item);
    }

    public void delete(ShopItem item, final DaoCallBackMethod<Void> callBack) {
        AsyncTask<ShopItem, Void, Boolean> task = new AsyncTask<ShopItem, Void, Boolean>() {

            @Override
            protected Boolean doInBackground(ShopItem... ShopItems) {

                ShopItem sl = ShopItems[0];

                Map<String, String> argsMap = new HashMap<String, String>();

                return fetcher.postData("items/" + sl.getId() + "/delete", argsMap);

            }

            @Override
            protected void onPostExecute(Boolean result) {
                super.onPostExecute(result);
                callBack.execute(null);
            }
        };

        task.execute(item);
    }
}
